package com.qys.livesMall.event.listener;

import com.qys.livesMall.app.user.event.GoldRecordEvent;
import com.qys.livesMall.app.user.event.LoginEvent;
import com.qys.livesMall.app.user.model.dto.sys.GoldRecordEventDTO;
import com.qys.livesMall.app.user.model.entity.UserShopStatistics;
import com.qys.livesMall.app.user.model.entity.UserStatisticsInfo;
import com.qys.livesMall.app.user.service.UserShopStatisticsService;
import com.qys.livesMall.app.user.service.UserStatisticsInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

/**
 * 用户代币监听
 */
@RequiredArgsConstructor
public class GoldRecordListener {

    private final UserStatisticsInfoService userStatisticsInfoService;
    @Async
    @Order
    @EventListener(GoldRecordEvent.class)
    public void saveUserStatisticsInfo(GoldRecordEvent goldRecordEvent){
        GoldRecordEventDTO goldRecordEventDTO = (GoldRecordEventDTO) goldRecordEvent.getSource();
        UserStatisticsInfo userStatisticsInfo=userStatisticsInfoService.selectByUserId(goldRecordEventDTO.getUserId());
        if (null != userStatisticsInfo){
            userStatisticsInfo.setTokens(goldRecordEventDTO.getGoldAmount());
            userStatisticsInfoService.updateById(userStatisticsInfo);
        }
    }
}
